package com.example.xiaozhi;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class myAdapter extends RecyclerView.Adapter<myAdapter.VH>{
    private List<AirCon>list;
    public myAdapter(List<AirCon>list){

        this.list=list;
    }
    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        View view =inflater.inflate(R.layout.list_item,parent,false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        holder.tvBrand.setText(list.get(position).getBrand());
        holder.tvMode.setText(list.get(position).getModeText());
        holder.tvTemp.setText(list.get(position).getTemp()+"℃");
        holder.ivMode.setImageDrawable(list.get(position).getModeImage());
        holder.ivMode1.setImageDrawable(list.get(position).getModeImage1());
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                       删除自带默认动画
                removeData(position);
            }
        });
    }

    @Override
    public int getItemCount() {

        return list.size();
    }
    public void removeData(int position) {
        //调用ArrayList的remove方法，从数据源list中删除指定位置position的数据。
        list.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    public class VH extends RecyclerView.ViewHolder{

        public TextView tvBrand;
        public TextView tvMode;
        public ImageView ivMode;
        public ImageView ivMode1;
        public TextView tvTemp;
        public ImageView ivDelete;
        public VH(@NonNull View itemView) {
            super(itemView);
            tvBrand=itemView.findViewById(R.id.tvBrand);
            tvMode=itemView.findViewById(R.id.tvMode);
            ivMode=itemView.findViewById(R.id.ivMode);
            tvTemp=itemView.findViewById(R.id.tvTemp);
            ivMode1=itemView.findViewById(R.id.ivMode1);
            ivDelete=itemView.findViewById(R.id.delete);
        }
    }
}
